// pages/jiyibi1/jiyibi1.js
const value = 0
const db = wx.cloud.database()

Page({
  data: {
    isincome:'true',
    date0: '请选择',
    multiArray: [['餐饮', '交通', '购物', '居家', '人情', '医疗', '娱乐', '学习', '金融', '其他'], ['三餐', '早餐', '中餐', '晚餐', '买菜', '水果', '零食', '下午茶', '烟酒饮品', '粮酒调料', '公交地铁', '打车', '飞机火车', '共享单车', '加油', '日用品', '衣帽鞋包', '护肤美妆', '首饰品', '数码', '电器', '家装', '水电煤', '话费', '网费', '房费', '物业', '维修', '送礼', '请客', '孝心', '亲密付', '药品', '保健', '治疗', '美妆', '休闲', '约会', '聚会', '游戏', '健身', '书籍', '培训', '网课', '考试', '房贷', '车贷', '购物分期', '手续费', '养卡', '旅游', '装修', '宝宝', '生意', '宠物', '还钱', '借出']],
    multiArray1: [['职业工资', '其他收入'], ['工资', '经营', '利息', '兼职', '奖金', '加班', '礼金', '抢红包', '意外来钱', '家里给钱', '中奖', '退税', '还钱']],
    objectMultiArray: [
      [
        {
          id: 0,
          name: '餐饮'
        },
        {
          id: 1,
          name: '交通'
        },
        {
          id: 2,
          name: '购物'
        },
        {
          id: 3,
          name: '居家'
        },
        {
          id: 4,
          name: '人情'
        },
        {
          id: 5,
          name: '医疗'
        },
        {
          id: 6,
          name: '娱乐'
        },
        {
          id: 7,
          name: '学习'
        },
        {
          id: 8,
          name: '金融'
        },
        {
          id: 9,
          name: '其他'
        }
      ], [
        {
          id: 0,
          name: '三餐'
        },{
          id: 1,
          name: '早餐'
        },{
          id: 2,
          name: '中餐'
        },{
          id: 3,
          name: '晚餐'
        },{
          id: 4,
          name: '买菜'
        },{
          id: 5,
          name: '水果'
        },{
          id: 6,
          name: '零食'
        },{
          id: 7,
          name: '下午茶'
        },{
          id: 8,
          name: '烟酒饮品'
        },{
          id: 9,
          name: '粮酒调料'
        },{
          id: 10,
          name: '公交地铁'
        },{
          id: 11,
          name: '打车'
        },{
          id: 12,
          name: '飞机火车'
        },{
          id: 13,
          name: '共享单车'
        },{
          id: 14,
          name: '加油'
        },{
          id: 15,
          name: '日用品'
        },{
          id: 16,
          name: '衣帽鞋包'
        },{
          id: 17,
          name: '护肤美妆'
        },{
          id: 18,
          name: '首饰品'
        },{
          id: 19,
          name: '数码'
        },{
          id: 20,
          name: '电器'
        },{
          id: 21,
          name: '家装'
        },{
          id: 22,
          name: '水电煤'
        },{
          id: 23,
          name: '话费'
        },{
          id: 24,
          name: '网费'
        },{
          id: 25,
          name: '房费'
        },{
          id: 26,
          name: '物业'
        },{
          id: 27,
          name: '维修'
        },{
          id: 28,
          name: '送礼'
        },{
          id: 29,
          name: '请客'
        },{
          id: 30,
          name: '孝心'
        },{
          id: 31,
          name: '亲密付'
        },{
          id: 32,
          name: '药品'
        },{
          id: 33,
          name: '保健'
        },{
          id: 34,
          name: '治疗'
        },{
          id: 35,
          name: '美妆'
        },{
          id: 36,
          name: '休闲'
        },{
          id: 37,
          name: '约会'
        },{
          id: 38,
          name: '聚会'
        },{
          id: 39,
          name: '游戏'
        },{
          id: 40,
          name: '健身'
        },{
          id: 41,
          name: '书籍'
        },{
          id: 42,
          name: '培训'
        },{
          id: 43,
          name: '网课'
        },{
          id: 44,
          name: '考试'
        },{
          id: 45,
          name: '房贷'
        },{
          id: 46,
          name: '车贷'
        },{
          id: 47,
          name: '购物分期'
        },{
          id: 48,
          name: '手续费'
        },{
          id: 49,
          name: '养卡'
        },{
          id: 50,
          name: '旅游'
        },{
          id: 51,
          name: '装修'
        },{
          id: 52,
          name: '宝宝'
        },{
          id: 53,
          name: '生意'
        },{
          id: 54,
          name: '宠物'
        },{
          id: 55,
          name: '还钱'
        },{
          id: 56,
          name: '借出'
        }
      ]
    ],
    objectMultiArray1: [
      [
        {
          id: 0,
          name: '职业工资'
        },{
          id: 1,
          name: '其他收入'
        }
      ], [
        {
          id: 0,
          name: '工资'
        },{
          id: 1,
          name: '经营'
        },{
          id: 2,
          name: '利息'
        },{
          id: 3,
          name: '兼职'
        },{
          id: 4,
          name: '奖金'
        },{
          id: 5,
          name: '加班'
        },{
          id: 6,
          name: '礼金'
        },{
          id: 7,
          name: '抢红包'
        },{
          id: 8,
          name: '意外来钱'
        },{
          id: 9,
          name: '家里给钱'
        },{
          id: 10,
          name: '中奖'
        },{
          id: 11,
          name: '退税'
        },{
          id: 12,
          name: '还钱'
        },
      ]
    ],
    multiIndex: [0, 0],
    multiIndex1: [0, 0],

    date1: '请选择',
    //textarea
    maxTextLen: 30,
    textLen: 0,
    contentStr: "",
    // test:{"餐饮":["三餐","早餐"],"交通":["事故","闯红灯"]}
  },
  click(e){
    console.log(e)
    if(e.currentTarget.dataset.name=="pay"){
      console.log("pay")
      this.setData({
        isincome:true
      })
    }else{
      console.log("income")
      this.setData({
        isincome:false
      })
    }
  },
  //输入是否合法
  passWdInput(e){
    console.log(e)
    console.log(e.detail.value)
    var value = e.detail.value
    let replaceArray = [];
      for (let i = 0; i < value.length; ++i) {//正则判断是否合法
        var textValue = (/^[0-9.]$/.test(value.charAt(i)));
        if (!textValue) {
          replaceArray.push(value.charAt(i));
        }
      }
      if (replaceArray.length != 0) {
        wx.showToast({
          title: '输入内容不合法',
          icon: 'none'
        })
        for (let j = 0; j < replaceArray.length; ++j) {
          value = value.replace(replaceArray[j], '')
          this.setData({
            value
          })
        }
      }
  },
  passWdBlur(e){
    let value = e.detail.value
    let splitArray = []
    splitArray = value.split(".")
    if(splitArray.length>=3){
      value = splitArray[0]+"."+splitArray[1]
    }
    console.log(value)
    // value = value.toFixed(2)
    this.setData({
      value
    })
  },
  //2联选择器1
  bindMultiPickerChange: function (e) {
    console.log(e)
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['三餐', '早餐', '中餐', '晚餐', '买菜', '水果', '零食', '下午茶', '烟酒饮品', '粮酒调料'];
            break;
          case 1:
            data.multiArray[1] = ['公交地铁', '打车', '飞机火车', '共享单车', '加油'];
            break;
          case 2:
            data.multiArray[1] = ['日用品', '衣帽鞋包', '护肤美妆', '首饰品', '数码', '电器', '家装'];
            break;
          case 3:
            data.multiArray[1] = ['水电煤', '话费', '网费', '房费', '物业', '维修'];
            break;
          case 4:
            data.multiArray[1] = ['送礼', '请客', '孝心', '亲密付'];
            break;
          case 5:
            data.multiArray[1] = ['药品', '保健', '治疗', '美妆'];
            break;
          case 6:
            data.multiArray[1] = ['休闲', '约会', '聚会', '游戏', '健身'];
            break;
          case 7:
            data.multiArray[1] = ['书籍', '培训', '网课', '考试'];
            break;
          case 8:
            data.multiArray[1] = ['房贷', '车贷', '购物分期', '手续费', '养卡'];
            break;
          case 9:
            data.multiArray[1] = ['旅游', '装修', '宝宝', '生意', '宠物', '还钱', '借出'];
            break;
        }
        data.multiIndex[1] = 0;
        break;
    }
    console.log(data.multiIndex);
    this.setData(data);
  },
  //2联选择器2
  bindMultiPickerChange1: function (e) {
    console.log(e)
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex1: e.detail.value
    })
  },
  bindMultiPickerColumnChange1: function (e) {
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray1: this.data.multiArray1,
      multiIndex1: this.data.multiIndex1
    };
    data.multiIndex1[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex1[0]) {
          case 0:
            data.multiArray1[1] = ['工资', '经营', '利息', '兼职', '奖金', '加班'];
            break;
          case 1:
            data.multiArray1[1] = ['礼金', '抢红包', '意外来钱', '家里给钱', '中奖', '退税', '还钱'];
            break;
        }
        data.multiIndex1[1] = 0;
        break;
    }
    console.log(data.multiIndex1);
    this.setData(data);
  },
  //时间
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date1: e.detail.value
    })
  },
  getWords(e) {
    let page = this;
    let maxTextLen = page.data.maxTextLen;
    let textLen = e.detail.value.length;
    page.setData({
      maxTextLen: maxTextLen,
      textLen: textLen,
      contentStr: e.detail.value
    });
  },
  // 提交表单
  bindFormSubmit(e){
    if(e.detail.value.money==""||e.detail.value.time=="请选择"){
      wx.showToast({
        icon: 'none',
        title: '填写未完整',
      })
      return
    }
    console.log(e)
    if(e.detail.value.pay== "pay"){
      console.log("pay")
      // db.collection('subben_pay').add({
      //   data:{
      //     money:e.detail.value.money,
      //     leibie:e.detail.value.leibie,
      //     time:e.detail.value.time,
      //     info:e.detail.value.info
      //   }
      // })
      // .then(res => {
      //   console.log('添加成功', res)
      wx.cloud.callFunction({
        name:"subben_pay",
        data:{
          money:e.detail.value.money,
          leibie:e.detail.value.leibie,
          time:e.detail.value.time,
          info:e.detail.value.info
        }
      }).then(res =>{
      console.log('subben_pay',res)
        wx.redirectTo({
          url: '../jiyibi1/jiyibi1',
        })
        wx.showToast({
          icon: 'none',
          title: '提交成功',
        })
      })
      .catch(res => {
        console.log('添加失败', res)
      })
      return
    }
    if(e.detail.value.income== "income"){
      console.log("income")
      // db.collection('subben_income').add({
      //   data:{
      //     money:e.detail.value.money,
      //     leibie:e.detail.value.leibie,
      //     time:e.detail.value.time,
      //     info:e.detail.value.info
      //   }
      // })
      wx.cloud.callFunction({
        name:"subben_income",
        data:{
          money:e.detail.value.money,
          leibie:e.detail.value.leibie,
          time:e.detail.value.time,
          info:e.detail.value.info
        }
      })
      .then(res => {
        console.log('添加成功', res)
        wx.redirectTo({
          url: '../jiyibi1/jiyibi1',
        })
      })
        wx.showToast({
          icon: 'none',
          title: '提交成功',
        })
      .catch(res => {
        console.log('添加失败', res)
      })
      return
    }
  },
  return(){
    wx.switchTab({
      url: '/pages/zhuye/zhuye'
    })
  }

})